﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DblogStudio.DAL.Models;

namespace DblogStudio.BLL
{
    public partial class BizCongDoanSanXuat
    {
        public static void GetStatisCanhBao(string maCongDoan, out int quaHan, out int trongHan, out int truocHan, string manv = "")
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var lsCTMDPC = db.PhanCongBaoTriDbSet.Where(e => e.MaNhanVien == manv).Select(e => e.MaChiTietMay).ToList();
                var cb = int.Parse(BizAppConfig.GetDSCanhBaoDesktop());
                if (string.IsNullOrEmpty(manv))
                    cb = 1000;
                DateTime dt = DateTime.Now.AddDays(cb);
                var ls = db.LichBaoTriDbSet.Where(e =>
                    e.ChiTietMay.BoPhanMay.MaySanXuat.CongDoanSanXuat.MaCongDoanSanXuat == maCongDoan && e.NgayBaoTri <= dt &&
                    (string.IsNullOrEmpty(manv) ? true : lsCTMDPC.Contains(e.MaChiTietMay)))
                    .ToList();
                trongHan = ls.Count(e => DateTime.Now.Date.Subtract(e.NgayBaoTri.Date).Days <= 0.3 * e.ChuKy && DateTime.Now.Date.Subtract(e.NgayBaoTri.Date).Days>=0);
                quaHan = ls.Count(e => DateTime.Now.Date.Subtract(e.NgayBaoTri.Date).Days > 0.3 * e.ChuKy);
                truocHan = ls.Count(e => DateTime.Now.Date.Subtract(e.NgayBaoTri.Date).Days < 0);
            }
        }


        /// <summary>
        /// Lấy danh sách tất cả các cơ sở đào tạo
        /// </summary>
        /// <returns></returns>
        public static List<CongDoanSanXuat> GetList(CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (db = CEMS4UG_DHKHContext.GetContext())
                {
                    return GetList(db);
                }
            else
            {
                return db.CongDoanSanXuatDbSet.OrderBy(e => e.TenCongDoanSanXuat).ToList();
            }
        }

        /// <summary>
        /// Lấy theo ID
        /// </summary>
        /// <param name="maCoSoDaoTao"></param>
        /// <returns></returns>
        public static CongDoanSanXuat GetByID(string maCongDoan, CEMS4UG_DHKHContext db = null)
        {
            if (db == null)
                using (db = CEMS4UG_DHKHContext.GetContext())
                {
                    return GetByID(maCongDoan, db);
                }
            else
            {
                return db.CongDoanSanXuatDbSet.Where(e => e.MaCongDoanSanXuat == maCongDoan).FirstOrDefault();
            }
        }



        /// <summary>
        /// Kiểm tra tên cơ sở đào tạo có trùng hay không
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool IsExistByName(string tenCongDoan)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                return db.CongDoanSanXuatDbSet.Where(e => e.TenCongDoanSanXuat == tenCongDoan).FirstOrDefault() != null ? true : false;
            }
        }

        public static CODE_RESULT_RETURN Add(CongDoanSanXuat obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                if (IsExistByName(obj.TenCongDoanSanXuat))
                    return CODE_RESULT_RETURN.TenTrung;
                obj.MaCongDoanSanXuat = Guid.NewGuid().ToString();
                try
                {
                    db.CongDoanSanXuatDbSet.Add(obj);
                    db.SaveChanges();
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData<CongDoanSanXuat>(nhatKy, null, obj.MaCongDoanSanXuat, LOAIDOITUONG.CÔNG_ĐOẠN_SẢN_XUẤT, LOAITHAOTAC.Thêm);
                    BizNhatKyHoatDong.Add(nhatKy, obj);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                catch
                {
                    return CODE_RESULT_RETURN.ThatBai;
                }
            }
        }

        public static CODE_RESULT_RETURN Update(CongDoanSanXuat obj)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var coSoDaoTao = db.CongDoanSanXuatDbSet.Where(e => e.MaCongDoanSanXuat == obj.MaCongDoanSanXuat).FirstOrDefault();
                if (coSoDaoTao != null)
                {
                    var ten = db.CongDoanSanXuatDbSet.Where(e => e.TenCongDoanSanXuat == obj.TenCongDoanSanXuat && e.MaCongDoanSanXuat != coSoDaoTao.MaCongDoanSanXuat).FirstOrDefault();
                    if (ten != null)
                        return CODE_RESULT_RETURN.TenTrung;
                    NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                    BizNhatKyHoatDong.InitData(nhatKy, coSoDaoTao, coSoDaoTao.MaCongDoanSanXuat, LOAIDOITUONG.CÔNG_ĐOẠN_SẢN_XUẤT, LOAITHAOTAC.Chỉnh_sửa);
                    coSoDaoTao.TenCongDoanSanXuat = obj.TenCongDoanSanXuat;
                    coSoDaoTao.GhiChu = obj.GhiChu;
                    db.SaveChanges();
                    BizNhatKyHoatDong.Add(nhatKy, coSoDaoTao);
                    return CODE_RESULT_RETURN.ThanhCong;
                }
                else
                {
                    throw new Exception("Đối tượng này không tồn tại");
                }
            }
        }

        public static CODE_RESULT_RETURN Delete(string maCongDoan)
        {
            using (var db = CEMS4UG_DHKHContext.GetContext())
            {
                var csdt = db.CongDoanSanXuatDbSet.Where(e => e.MaCongDoanSanXuat == maCongDoan).FirstOrDefault();
                if (csdt != null)
                {
                    if (!(csdt.ListMaySanXuat != null && csdt.ListMaySanXuat.Count > 0))
                    {
                        db.CongDoanSanXuatDbSet.Remove(csdt);
                        db.SaveChanges();
                        NhatKyHoatDong nhatKy = new NhatKyHoatDong();
                        BizNhatKyHoatDong.InitData<CongDoanSanXuat>(nhatKy, csdt, csdt.MaCongDoanSanXuat, LOAIDOITUONG.CÔNG_ĐOẠN_SẢN_XUẤT, LOAITHAOTAC.Xóa);
                        BizNhatKyHoatDong.Add<CongDoanSanXuat>(nhatKy, null);
                        return CODE_RESULT_RETURN.ThanhCong;
                    }
                    else
                        return CODE_RESULT_RETURN.ThatBai;
                }
                else
                {
                    return CODE_RESULT_RETURN.ThatBai;
                }
            }
        }
    }
    public class Temp
    {
        public string text { get; set; }
        public string value { get; set; }
    }
}
